// ==UserScript==
// @name        哆啦美计划
// @namespace   drama
// @include     https://user/wanxiangyun.net/db/list/1
// @version     1
// @grant       none
// ==/UserScript==
function run(start, end) {
  $('#download-range-start').val(start);
  $('#download-range-end').val(end);
  $('#download-patent').trigger('click');
}
function gamma(n) {
  return n == 0 ? 1 : n * gamma(n - 1);
};
function poisson(means) {
  var x = 0;
  var b = 1;
  var c = Math.exp( - means);
  do {
    var u = Math.random();
    b *= u;
    if (b >= c)
    x++;
  } while (b >= c);
  return x;
}
function prepare() {
	$("#chkall").trigger("click");
	$("#down-option-9").trigger("click");
	$("#down-option-10").trigger("click");
};

function readStep(fileName, offset, step){
  $.getJSON('http://127.0.0.1:8080/recordServer/readBy?jsoncallback=?', {
    'fileName': fileName,
    'step': step,
    'offset': offset
  }, function (jsonData) {
    if (jsonData == 'ERROR')
    return;
    $('#search').val(jsonData);
    $('#searchBtn').trigger('click');
    $('#findResult').trigger('click');
  };
}

function read(fileName, offset, step, maxSize) {
  if (offset > maxSize)
  return;
var timeline = 0;
for(;offset<maxSize;offset+=step){
timeline += poisson(10) * 1000;
(function (start, end) {
      setTimeout(function () {
        readStep(fileName1, offset1, step1);
      }, timeline);
    }) (fileName, offset,step);
  };

}

};
function resultDownload() {
  prepare();
  var max = parseInt(document.getElementById('selected').innerText.replace(/[^\d]+/g, ''));
  var timeline = 0;
  for (var i = 1; i <= max; i += 500) {
    timeline += poisson(10) * 1000;
    (function (start, end) {
      setTimeout(function () {
        run(start, end);
      }, timeline);
    }) (i, i + 499 > max ? max : i + 499);
  };
};
$(document).ready(resultDownload());
